<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出授权') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入授权') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'bas:cus:basUserCusPerson:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('添加授权') }}
        </a-button>
        <a-popconfirm
          title="是否确认取消授权？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleCancel()"
        >
          <a-button type="danger" v-auth="'setting:price:edit'">
            <Icon icon="ant-design:close-outlined" /> {{ t('取消授权') }}
          </a-button>
        </a-popconfirm>
      </template>
      <template #firstColumn="{ record }"> </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasCusBasUserCusPersonList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    basUserCusPersonDelete,
    basUserCusPersonListData,
    basUserCusPersonBatchDelete,
  } from '/@/api/bas/cus/basUserCusPerson';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';

  const { t } = useI18n('bas.cus.basUserCusPerson');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('文员门店授权管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 4, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('文员帐号'),
        field: 'user.loginCode',
        component: 'Input',
      },
      {
        label: t('文员名称'),
        field: 'user.userName',
        component: 'Input',
      },
      {
        label: t('客户名称'),
        field: 'basCus.name',
        component: 'Input',
      },
      {
        label: t('客户编码'),
        field: 'cusCode',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('文员帐号'),
      dataIndex: 'user.loginCode',
      key: 'u1.login_code',
      width: 100,
      align: 'left',
      sorter: true,
    },
    {
      title: t('文员名称'),
      dataIndex: 'user.userName',
      key: 'u1.user_name',
      width: 100,
      align: 'left',
      sorter: true,
    },
    {
      title: t('门店编码'),
      dataIndex: 'cusCode',
      key: 'cus_code',
      width: 110,
      align: 'left',
      sorter: true,
    },
    {
      title: t('门店名称'),
      dataIndex: 'basCus.name',
      width: 250,
      key: 'cus.name',
      align: 'left',
      sorter: true,
    },
    {
      title: t('导入时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      width: 150,
      align: 'left',
      sorter: true,
    },
  ];

  const actionColumn: BasicColumn = {
    width: 100,
    actions: (record: Recordable) => [
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除授权'),
        popConfirm: {
          title: t('是否确认删除门店授权'),
          confirm: handleDelete.bind(this, { userCode: record.userCode, cusCode: record.cusCode }),
        },
        auth: 'bas:cus:basUserCusPerson:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: basUserCusPersonListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/bas/cus/basUserCusPerson/exportData',
      target: '_self',
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const res = await basUserCusPersonDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 取消授权
  async function handleCancel() {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.userCode + '#' + item.cusCode + ',';
      });
      const res = await basUserCusPersonBatchDelete({ selIds: selIds.value });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  function handleSuccess() {
    reload();
  }
</script>
